import CBLModel from '../dbs/models/CBlate'
import informtModel from '../dbs/models/inform'
import visitDorm from '../dbs/models/visit'
import { mGetDate } from './utils/getMonthDays'
import { tower_obj } from '../interface/utils/web_scoket'


// 获取最新通知
async function getInfromData (tower) {
  let inform_newest = []
  try {
    inform_newest = await informtModel.find(
      {
        tower,
        time: {
          "$gte": new Date(new Date().getFullYear() + ''),
          "$lte": new Date(`${new Date().getFullYear()}-12-31`)
        }
      }, null,
      {
        limit: 5
      }
    ).sort({ "time": -1 })
  } catch (error) {
    throw error
  }
  return inform_newest

}
// 获取来访人员前几条
async function getVisiterData (tower) {
  let visiter = []
  try {
    visiter = await visitDorm.find({
      tower
    }, null,
      {
        limit: 6
      }).sort({ "visitTime": -1 })
  } catch (error) {
    throw error
  }
  return visiter
}
// 获取最新6位晚归人员
async function get_come_back_late (tower) {
  let cpme_back_late = []
  try {
    cpme_back_late = await CBLModel.find({
      tower
    }, null,
      {
        limit: 6
      }).sort({ "BCLtime": -1 })
  } catch (error) {
    throw error
  }
  return cpme_back_late
}
// 获取本月来访人员总数
async function getVisitedMonthCount (tower) {
  let month_visited_count = []
  let year = new Date().getFullYear() + ''
  let month = new Date().getMonth() + ''
  let month_last_day = mGetDate() + ''
  try {
    month_visited_count = await visitDorm.find(
      {
        tower,
        visitTime: {
          "$gte": new Date(year, month, 1),
          "$lte": new Date(year, month, month_last_day)
        }
      }
    )
  } catch (error) {
    throw error
  }
  return month_visited_count

}
// 获取本月晚归总人数
async function getComeBackLateMonthCount (tower) {
  let month_come_back_late_count = []
  let year = new Date().getFullYear() + ''
  let month = new Date().getMonth() + ''
  let month_last_day = mGetDate() + ''
  try {
    month_come_back_late_count = await CBLModel.find(
      {
        tower,
        BCLtime: {
          "$gte": new Date(year, month, 1),
          "$lte": new Date(year, month, month_last_day)
        }
      }
    )
  } catch (error) {
    throw error
  }
  return month_come_back_late_count

}
export async function wsData () {
  let result = {}
  let tower = tower_obj.tower
  try {
    const inform_newest = await getInfromData(tower)
    const visited_recently = await getVisiterData(tower)
    const CBL = await get_come_back_late(tower)
    const month_visited_count = await getVisitedMonthCount(tower)
    const month_come_back_late_count = await getComeBackLateMonthCount(tower)
    // console.log(month_come_back_late_count)
    result['informs'] = inform_newest
    result['visited'] = visited_recently
    result['come_back_late'] = CBL
    result['month_visited_count'] = month_visited_count
    result['month_come_back_late_count'] = month_come_back_late_count
  } catch (error) {
    throw error
  }
  return result
}